package com.engine.workflow.customServer.impl;

import com.alibaba.fastjson.JSONObject;
import com.weaverboot.frame.ioc.anno.classAnno.WeaIocReplaceComponent;
import com.weaverboot.frame.ioc.anno.methodAnno.WeaReplaceAfter;
import com.weaverboot.frame.ioc.handler.replace.weaReplaceParam.impl.WeaAfterReplaceParam;
import weaver.conn.RecordSet;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author Huangwm
 * @Date 2022-06-08
 * @Version 1.0
 * @Description TODO 修改/api/workflow/reqform/getRequestLogList返回的数据  to 林毅
 **/
@WeaIocReplaceComponent("NewGetRequestLogList")
public class GetRequestLogList {
    private LogTool logTool = new LogTool("/log/huangwm/rewriteApi/workflow/GetRequestLogList",false);

    @WeaReplaceAfter(value = "/api/workflow/reqform/getRequestLogList",order = 1)
    public String after(WeaAfterReplaceParam weaAfterReplaceParam) {
        String data = weaAfterReplaceParam.getData();//这个就是接口执行完的报文
        logTool.writeLog("===========拦截/api/workflow/reqform/getRequestLogList===========");
        logTool.writeLog(data);
        Map<String, Object> dataMap = JSONObject.parseObject(data);
        Map<String, Object> params = weaAfterReplaceParam.getParamMap();

        List<Map<String,Object>> loglist = (List<Map<String, Object>>) dataMap.get("loglist");
        List<Map<String,Object>> newLoglist = new ArrayList<>();
        if(loglist.size()>0){
            RecordSet rs1 = new RecordSet();
            RecordSet rs2 = new RecordSet();
            for(Map<String,Object> info : loglist){
                Map<String,Object> loginfo = new HashMap<>();
                for(Map.Entry<String,Object> entry : info.entrySet()){
                    loginfo.put(entry.getKey(),entry.getValue());
                    String logid = String.valueOf(info.get("logid"));
                    String displayid = String.valueOf(info.get("displayid"));//操作的人员id
                    String olddisplaydepid = String.valueOf(info.get("displaydepid"));
                    String olddisplaydepname = String.valueOf(info.get("displaydepname"));
                    //根据logid查询操作日志表，获取requestid和nodeid以及operator
                    String depfield = getLogInfoV2(rs1,rs2,logid);
                    String depName = getDepName(rs1,depfield);
                    //如果该条签字意见记录对应的审批信息有维护则替换内容
                    loginfo.put("displaydepid",(!depfield.equals("-1") && !depfield.equals(""))?depfield:olddisplaydepid);
                    loginfo.put("displaydepname",(!depName.equals("null") && !depfield.equals(""))?depName:olddisplaydepname);
                }
                newLoglist.add(loginfo);
            }
        }
        dataMap.put("loglist",newLoglist);
        logTool.writeLog(JSONObject.toJSONString(dataMap));
        return JSONObject.toJSONString(dataMap);
    }

    /**
     * 根据logid获取workflow_requestlog
     * @param rs
     * @param logid
     * @return
     */
    private Map<String,Object> getLogInfo(RecordSet rs, String logid){
        Map<String,Object> info = new HashMap<>();
        String sql = "select * from workflow_requestlog where logid="+logid;
        rs.execute(sql);
        if(rs.next()){
            info.put("requestid",rs.getInt("requestid"));
            info.put("workflowid",rs.getInt("workflowid"));
            info.put("nodeid",rs.getInt("nodeid"));
            info.put("operator",rs.getInt("operator"));
        }
        return info;
    }

    /**
     * 根据logid获取workflow_requestlog对应维护数据中的部门字段值
     * @param rs1
     * @param rs2
     * @param logid
     * @return
     */
    private String getLogInfoV2(RecordSet rs1, RecordSet rs2,String logid){
        String sql = "select * from workflow_requestlog where logid="+logid;
        rs1.execute(sql);
        if(rs1.next()){
            int requestid = rs1.getInt("requestid");
            int wfid = rs1.getInt("workflowid");
            int nodeid = rs1.getInt("nodeid");
            int userid= rs1.getInt("operator");

            String sql2 = "select wfid,depfield,nodeid,userid from uf_hwm_LogConfig_dt1 dt left join uf_hwm_LogConfig m on m.id=dt.mainid\n" +
                    "where wfid="+wfid+" and nodeid="+nodeid+" and userid="+userid;
            rs2.execute(sql2);
            if(rs2.next()){
                String depfield = rs2.getString("depfield").split("_hwm_")[1];
                String tablename = getRequestComInfo(rs1,requestid+"","tablename");
                rs1.execute("select "+ depfield +" from "+tablename+" where requestid="+requestid);
                if(rs1.next())
                    return rs1.getString(depfield);
                else
                    return "-1";
            }
        }
        return "-1";
    }



    /**
     * 根据requestid获取对应表单相关信息
     * tablename,workflowid
     * @param rs
     * @param requestid
     * @param field
     * @return
     */
    private String getRequestComInfo(RecordSet rs,String requestid,String field) {
        String msg = "";
        if (!"".equals(requestid)) {
            rs = new RecordSet();
            String sql = " select wb2.tablename,wb1.id as wfid,wr.workflowid from workflow_requestbase wr left join workflow_base wb1 on wb1.id=wr.workflowid" +
                    "\n left join workflow_bill wb2 on wb1.formid=wb2.id where requestid=" + requestid;
            rs.execute(sql);
            if (rs.next()) msg = rs.getString(field);
        }
        return msg;
    }

    public List<Map<String,String>> getMoelConfig(RecordSet rs){
        List<Map<String,String>> config = new ArrayList<>();
        String sql = "select wfid,depfield,nodeid,userid from uf_hwm_LogConfig_dt1 dt left join uf_hwm_LogConfig m on m.id=dt.mainid ";
        rs.execute(sql);
        while (rs.next()){
            Map<String,String> map = new HashMap<>();
            map.put("wfid",rs.getString("wfid"));
            map.put("depfield",rs.getString("depfield"));
            map.put("nodeid",rs.getString("nodeid"));
            map.put("userid",rs.getString("userid"));
            config.add(map);
        }
        return config;
    }

    /**
     * 获取部门名称
     * @param rs
     * @param id
     * @return
     */
    public String getDepName(RecordSet rs,String id){
        if(!id.isEmpty()){
            rs.execute("select departmentname from hrmdepartment where id="+id);
            if(rs.next())
                return rs.getString("departmentname");
            else
                return "null";
        }
        return "null";
    }
}
